package com.ruoyi.report.forms.service.impl;

import com.ruoyi.report.forms.domain.vo.ShiftsContentVO;
import com.ruoyi.report.forms.mapper.PatrolInspectionRecordMapper;
import com.ruoyi.report.forms.service.IPatrolInspectionChangeShiftsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 巡检交接班管理Service业务层处理
 *
 * @author mashuang
 * @date 2022-08-22
 */
@Service
public class PatrolInspectionChangeShiftsServiceImpl implements IPatrolInspectionChangeShiftsService {

    @Autowired
    private PatrolInspectionRecordMapper patrolInspectionRecordMapper;


    /**
     * 根据场站及日期 查询时间段内的交班人及工作内容
     * 当前日期：08:00-16:00 | 16:00-00:00
     * 当前日期的第二天：00:00-08:00
     *
     * @param position
     * @param date
     * @return
     */
    @Override
    public List<ShiftsContentVO> selectShiftsInfo(String position, String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //结果集
        List<ShiftsContentVO> list = new ArrayList<>();
        try {
            //获取参数日期的第二日
            Date curDate = sdf.parse(date);
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(curDate);
            calendar.add(calendar.DATE, 1);
            String nextDate = sdf.format(calendar.getTime());
            //获取五个时间段
            String[] dateTimes = new String[]{date + " 00:00:00", date + " 08:00:00", date + " 16:00:00", nextDate + " 00:00:00", nextDate + " 08:00:00"};
            //查询每个时间段的交班人及工作内容
            for (int i = 1; i < dateTimes.length - 1; i++) {
                //根据场站及日期 查询时间段内的交班人及工作内容
                ShiftsContentVO vo = patrolInspectionRecordMapper.getShiftsInfo(position, dateTimes[i], dateTimes[i + 1], dateTimes[i - 1]);
                vo.setTimeRange(dateTimes[i] + "~" + dateTimes[i + 1]);
                list.add(vo);
            }
        } catch (Exception e) {
            System.out.println("转换日期类型异常：" + e.getMessage());
        }
        return list;
    }

    public List<ShiftsContentVO> selectShiftsInfoForMonth(String position, String month) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<ShiftsContentVO> list = new ArrayList<>();
        try {
            String startTime = month + "-01" + " 08:00:00";
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(sdf.parse(startTime));
            calendar.add(calendar.MONTH, 1);
            String endTime = sdf.format(calendar.getTime()) + " 08:00:00";

            list = patrolInspectionRecordMapper.getShiftsInfoMonth(position, startTime, endTime);
        } catch (ParseException e) {
            System.out.println("转换日期类型异常：" + e.getMessage());
        }

        return list;
    }
}
